package com.qxmly.fm.adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.qxmly.fm.R;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Q
 * Date:2021/11/29$
 **/
public class DetailAdapter extends RecyclerView.Adapter<DetailAdapter.InnerHolder> {

    private List<Track> mDetailData = new ArrayList<>();

    private SimpleDateFormat mUpdateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat mDurationFormat= new SimpleDateFormat("mm:ss");
    private OnItemClickListener mOnItemClickListener =null;
    @NonNull
    @Override
    public DetailAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_detail,parent,false);
        return new InnerHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull DetailAdapter.InnerHolder holder, @SuppressLint("RecyclerView") int position) {
        View itemView = holder.itemView;
        TextView orderTV = itemView.findViewById(R.id.order);
        TextView titleTv =itemView.findViewById(R.id.main_title);
        TextView playCount = itemView.findViewById(R.id.detail_item_play_count);
        TextView durationTv = itemView.findViewById(R.id.detail_item_duration);
        TextView updateDateTv = itemView.findViewById(R.id.time_text);

        Track track = mDetailData.get(position);
        orderTV.setText(position+"");
        titleTv.setText(track.getTrackTitle());
        playCount.setText(track.getPlayCount()+"");

        int duration = track.getDuration()*1000;
        String durationFormat= mDurationFormat.format(duration);
        durationTv.setText(durationFormat);

        String updateTimeText = mUpdateFormat.format(track.getUpdatedAt());
        updateDateTv.setText(updateTimeText);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(v.getContext(),"点击了" + position + "item" , Toast.LENGTH_SHORT).show();
                if(mOnItemClickListener!=null){
                    mOnItemClickListener.onItemClick(mDetailData,position);
                }

            }
        });

    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }



    public interface OnItemClickListener {
        void onItemClick(List<Track> detailData, int position);
    }


    @Override
    public int getItemCount() {
        return mDetailData.size();
    }

    public void setData(List<Track> tracks) {
        mDetailData.clear();
        //添加新数据
        mDetailData.addAll(tracks);
        //更新UI
        notifyDataSetChanged();
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
